package main

import (
	"context"

	pb "gofw-im/lib/grpc"
	"gofw-im/lib/logger"
	"gofw-im/lib/net"
	"gofw-im/lib/proto"

	"google.golang.org/grpc"
)

var (
	logicRpcClient pb.LogicClient
)

func InitLogicRpc() (err error) {
	_, addr, err := net.ParseNetwork(conf.LogicConfig.Addr)
	conn, err := grpc.Dial(addr, grpc.WithInsecure())
	if err != nil {
		logger.Error.Printf("connect err rpc comet: %s", err)
	}
	//defer conn.Close() // 不需要关闭，放到池子里需要用的时候获取
	logicRpcClient = pb.NewLogicClient(conn)
	return
}

func connect(connArg *proto.ConnArg) (uid string, err error) {
	logger.Info.Printf("comet logic rpc logicRpcClient %v:", logicRpcClient)
	connReply, err := logicRpcClient.Connect(context.Background(), &pb.ConnArg{
		Auth:     connArg.Auth,
		RoomId:   connArg.RoomId,
		ServerId: connArg.ServerId,
	})
	if err != nil {
		logger.Error.Printf("failed to call: %v", err)
		return
	}
	uid = connReply.Uid
	logger.Info.Printf("comet logic uid :%s", connReply.Uid)
	return
}

func disconnect(disconnArg *proto.DisconnArg) (err error) {
	disconnReply, err := logicRpcClient.Disconnect(context.Background(), &pb.DisconnArg{
		RoomId: disconnArg.RoomId,
		Uid:    disconnArg.Uid,
	})
	if err != nil {
		logger.Error.Printf("failed to call: %v", err)
		return
	}
	logger.Info.Printf("comet logic has :%s", disconnReply.Has)
	return
}
